/**
 * swapImage - jQuery plugin for swapping image(s)
 *
 * Copyright (c) 2008 tszming (tszming@gmail.com)
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Enable image swapping, requires metadata plugin.
 *
 * @example $.swapImage(".swapImage");
 * @desc Enable image swapping for all images with CSS class name equal to "swapImage", e.g.
 *	<img class="swapImage {src: 'images/new.gif'}" src="images/old.gif" />
 *
 * @example $.swapImage(".swapImageDisjoint");
 * @desc Enable image swapping for images with id = "img1" and "img2", when "img3" was selected, e.g.
 *	<img id="img1" class="{src: 'images/a.gif'}" src="images/1.gif" />
 *	<img id="img2" class="{src: 'images/b.gif'}" src="images/2.gif" />
 *	<img id="img3" class="swapImageDisjoint {sin: ['#img1', '#img2'], sout: ['#img1', '#img2']}" src="images/3.gif" />
 *
 * @param i Images to be selected.
 * @param preload Preload the image, default = true.
 * @param repeat Repeat the effect, default = true.
 * @param swapInEvent Event for swap In.
 * @param swapOutEvent Event for swap Out.
 *
 * @name $.swapImage
 * @cat Plugins/SwapImage
 * @author tszming (tszming@gmail.com)
 * @version 1.0.2
 */
jQuery.swapImage = function(i, preload, repeat, swapInEvent, swapOutEvent) {

    jQuery.swapImage.preload = function() {
        var data = jQuery(this).metadata();
        if (typeof data.src != "undefined") {
            var img = new Image();
            img.src = data.src;
        }
    };

    jQuery.swapImage.swapIn = function() {
	    jQuery.swapImage.swap(this, "sin");
    };

    jQuery.swapImage.swapOut = function() {
	    jQuery.swapImage.swap(this, "sout");
    };

    jQuery.swapImage.swap = function(obj, a) {
        var data = jQuery(obj).metadata();
        if (typeof data[a] != "undefined") {
            for (var i = 0; i < data[a].length; i++) {
                jQuery(data[a][i]).each(jQuery.swapImage._swap);
            }
        } else {
			jQuery.swapImage._swap.call(obj);
        }
    };

    jQuery.swapImage._swap = function() {
        var data = jQuery(this).metadata();
        if (typeof data.src != "undefined") {
            var tmp = data.src;
            data.src = this.src;
            this.src = tmp;
        }
    };

    jQuery(document).ready(function() {

	    if (typeof repeat == "undefined") {
    	    repeat = true;
	    }

	    if (typeof preload == "undefined") {
    	    preload = true;
	    }

        if (typeof swapInEvent == "undefined" && typeof swapInEvent == "undefined") {
            swapInEvent = "mouseenter";
            swapOutEvent = "mouseleave";
        }

        if (repeat) {
            if (typeof swapOutEvent != "undefined") {
                jQuery(i).bind(swapInEvent, jQuery.swapImage.swapIn).bind(swapOutEvent, jQuery.swapImage.swapOut);
            } else {
                jQuery(i).bind(swapInEvent, jQuery.swapImage.swapIn);
            }
        } else {
            jQuery(i).one(swapInEvent, jQuery.swapImage.swapIn);
        }

        if (preload) {
            jQuery(i).each(jQuery.swapImage.preload);
        }
    });
};